package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.approve.api.IApl_decision_recordFeign;
import com.power.sifa.approve.model.*;
import com.power.sifa.approve.model.pageInfo.Apl_decision_recordPageInfo;
import com.power.sifa.approve.model.pojo.vo.MainCaseInfoVO;
import com.power.sifa.approve.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   干预行政复议活动记录控制器
 */

@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_decision_recordController",tags={"干预行政复议活动记录"})
public class Apl_decision_recordController implements IApl_decision_recordFeign {

    @Resource
    private Apl_decision_recordService apl_decision_recordService;
    @Resource
    private Apl_mainService apl_mainService;
    @Resource
    private Apl_admreviewService apl_admreviewService;
    @Resource
    private Apl_applicantService apl_applicantService;
    @Resource
    private Apl_agentService apl_agentService;

    @Resource
    private Apl_targetService apl_targetService;

    @Resource
    private Apl_thirdperService apl_thirdperService;

    
    @Override
    @ApiOperation("查询干预行政复议活动记录分页列表")
    @SaCheckPermission("apl_decision_record_list")
    public ResponseDTO list(Apl_decision_recordPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_decision_record> page = this.apl_decision_recordService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_decision_record apl_decision_record : page) {
                apl_decision_record.geneText();
                list.add(apl_decision_record);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看干预行政复议活动记录")
    @SaCheckPermission("apl_decision_record_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_decision_record apl_decision_record  = apl_decision_recordService.getById(id);
        if (apl_decision_record != null) {
            apl_decision_record.geneText();
        }
        return ResponseDTO.succData(apl_decision_record);
    }

    @Override
    @ApiOperation("删除干预行政复议活动记录")
    @SaCheckPermission("apl_decision_record_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_decision_recordService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改干预行政复议活动记录")
    @SaCheckPermission("apl_decision_record_save")
    public ResponseDTO save(Apl_decision_record apl_decision_record) {
        Assert.notNull(apl_decision_record, "apl_decision_record不能为空");
        apl_decision_recordService.saveOrUpdate(apl_decision_record);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除干预行政复议活动记录")
    @SaCheckPermission("apl_decision_record_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_decision_recordService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_decision_record_list")
    public void output(HttpServletResponse response, Apl_decision_recordPageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "干预行政复议活动记录";
        String[] heads = {"序号","applyid","faapplyid","是否存在该情况","干预人信息","干预人其他情况","干预内容信息","干预内容其他情况","具体情况","签名","签名时间","主办人还是协办人"};
        String[] fields = {"id","applyid","faapplyid","isexist","personradio","personother","contentradio","contentother","detail","sign","singdate","signtype"};
        Page<Apl_decision_record> page = this.apl_decision_recordService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "干预行政复议活动记录";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"序号","applyid","faapplyid","是否存在该情况","干预人信息","干预人其他情况","干预内容信息","干预内容其他情况","具体情况","签名","签名时间","主办人还是协办人"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyid","faapplyid","isexist","personradio","personother","contentradio","contentother","detail","sign","singdate","signtype"};
        List<Apl_decision_record> list = (List<Apl_decision_record>)ExcelUtil.Import(fields, file, Apl_decision_record.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_decision_recordService.saveAll(list);
        }
        return ResponseDTO.succ();
	}


    /**
     * 查询还未决定的记录
     * @param faapplyid 父节点的applyid
     * @return
     * @throws IOException
     */
    @Override
    public ResponseDTO getNoDecisionRecords(String faapplyid) {
        Assert.isTrue(StringUtils.hasText(faapplyid), "faapplyid不能为空");
        List<Apl_decision_record> recordList = this.apl_decision_recordService.getNoDecisionRecords(faapplyid);
        return ResponseDTO.succData(recordList);
    }


    /**
     * 主办人查询还未决定的记录
     * @param faapplyid
     * @param applyid
     * @return
     */

    @Override
    public ResponseDTO getNoDecisionMainRecords(String faapplyid, String applyid) {
        Assert.isTrue(StringUtils.hasText(faapplyid), "faapplyid不能为空");
        Assert.isTrue(StringUtils.hasText(applyid), "applyid不能为空");
        List<Apl_decision_record> recordList = this.apl_decision_recordService.getNoDecisionMainRecords(faapplyid, applyid);
        return ResponseDTO.succData(recordList);
    }


    @Override
    public ResponseDTO getNoDecisionSubRecords(String faapplyid, String applyid) {
        Assert.isTrue(StringUtils.hasText(faapplyid), "faapplyid不能为空");
        Assert.isTrue(StringUtils.hasText(applyid), "applyid不能为空");
        List<Apl_decision_record> recordList = this.apl_decision_recordService.getNoDecisionSubRecords(faapplyid, applyid);
        return ResponseDTO.succData(recordList);
    }

    @Override
    public ResponseDTO getCaseMainInfo(String faapplyid) {
        Assert.isTrue(StringUtils.hasText(faapplyid), "faapplyid不能为空");
        Apl_main main = this.apl_mainService.getById(faapplyid);
        Apl_admreview admreview = this.apl_admreviewService.getById(faapplyid);
        List<Apl_applicant> applicantList = this.apl_applicantService.getApplicantByApplyid(faapplyid);
        List<Apl_agent> agentList = this.apl_agentService.getAgentByApplyid(faapplyid);
        List<Apl_target> targetList = this.apl_targetService.getTargetListByApplyid(faapplyid);
        List<Apl_thirdper> thirdperList = this.apl_thirdperService.getThirdListByApplyid(faapplyid);
        MainCaseInfoVO caseInfoVO = new MainCaseInfoVO().builder().mainInfo(main).admreview(admreview).applicantList(applicantList).agentList(agentList).targetList(targetList).thirdperList(thirdperList).build();
        return ResponseDTO.succData(caseInfoVO);
    }

}
